//
//  BrandViewController.m
//  LocomotiveGuards
//
//  Created by zwj on 15-11-23.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "BrandViewController.h"
#import "CarPhotoViewController.h"
#import "GDataXMLNode.h"
@interface BrandViewController () <UITextFieldDelegate>
{
    NSMutableArray *_brandArray;
    UIButton *_btnLast;
    UIButton *_btnNext;
    UITextField *_fieldInput;
    UILabel *_nameLabel;
}
@end

@implementation BrandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitle:@"注册-车辆验证"];
    [self basicXml];
    [self configView];
}

- (void)basicXml{
    _brandArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"carBrand_data" ofType:@"xml"];
   
    
    
    if (path.length == 0) {
        NSLog(@"没有读到资源!");
        return;
    }
    
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
    GDataXMLElement *rootElement = [document rootElement];
    NSArray *brands = [rootElement elementsForName:@"item"];
    for (int i=0; i<brands.count; i++) {
        [_brandArray addObject:[brands[i] stringValue]];
    }
}

- (void)configView
{
    //品牌label
    _nameLabel = [self createLabelFrame:CGRectMake(30*W, 110*H, SCREEN_WIDTH-60*W, 30*H) title:@"品牌" font:[UIFont systemFontOfSize:26]];
    [self.view addSubview:_nameLabel];
    
    //创建品牌按钮
    [self createBrandBtns];
    
    //手动输入框
    _fieldInput = [self createTextFieldFrame:CGRectMake(50*W, CGRectGetMaxY(_nameLabel.frame)+40*H+45*H*3+40*H, SCREEN_WIDTH-100*W, 30*H) title:nil placeholder:@"请填写其他品牌"];
    _fieldInput.returnKeyType = UIReturnKeyDone;
    _fieldInput.delegate = self;
    [self.view addSubview:_fieldInput];
    
    //上一步按钮
    _btnLast = [self addLastBtnTarget:self action:@selector(btnLastClicked:)];
    [self enableButton:_btnLast];
    
    //下一步按钮
    _btnNext = [self addNextBtnTarget:self action:@selector(btnNextClicked:)];
    [self disableButton:_btnNext];
}

//循环创建品牌按钮
- (void)createBrandBtns
{
    CGFloat width = (SCREEN_WIDTH-90*W)/3;
    CGFloat height = 45*H;
    for (NSInteger i=0; i<_brandArray.count; i++) {
        //行
        int row = i/3;
        //列
        int col = i%3;
        UIButton *btnBrand = [self createBtnFrame:CGRectMake(45*W+width*col, CGRectGetMaxY(_nameLabel.frame)+40*H+height*row, width, height) title:_brandArray[i] bgImageName:nil selectedImageName:nil target:self action:@selector(btnBrandClicked:)];
        btnBrand.titleLabel.font = [UIFont systemFontOfSize:20];
        [btnBrand setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.view addSubview:btnBrand];
    }
}

#pragma mark - 点击事件
//上一步，返回上一个界面
- (void)btnLastClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//下一步，进入新页面
- (void)btnNextClicked:(id)sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:_fieldInput.text forKey:@"brandName"];
    CarPhotoViewController *carVC = [CarPhotoViewController new];
    [self.navigationController pushViewController:carVC animated:YES];
}

//品牌按钮点击事件
- (void)btnBrandClicked:(UIButton *)btn
{
    _fieldInput.text = btn.titleLabel.text;
    if (_fieldInput.text != nil && _fieldInput.text.length != 0) {
        [self enableButton:_btnNext];
    }
}

//收起键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_fieldInput resignFirstResponder];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_fieldInput resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSString *tempString = [_fieldInput.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([tempString isEqualToString:@""]) {
        [self disableButton:_btnNext];
    }
    else {
        [self enableButton:_btnNext];
    }
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
